package terra.network.packet.server;

import org.jboss.netty.buffer.ChannelBuffer;

import terra.network.packet.Packet;

public class PlayerInfo extends Packet {

	public float x,y;
	public int w,h;
	public int hp,maxHp,str,con,dex,breath,maxBreath,atk,def,critChance,critPower;
	public long exp;
	public byte level;
	public int oId;
	
	public String name;
	
	public PlayerInfo() {
	}

	@Override
	public ChannelBuffer toBuffer() {
		byte[] n = stringToBytes(name);
		ChannelBuffer buf = getNewBuffer(78+n.length);
		buf.writeByte(getId());
		buf.writeInt(73+n.length);
		
		buf.writeInt(oId);	
		buf.writeFloat(x);
		buf.writeFloat(y);	
		buf.writeInt(w);	
		buf.writeInt(h);
		
		buf.writeInt(hp);	
		buf.writeInt(maxHp);
		buf.writeInt(str);	
		buf.writeInt(con);
		buf.writeInt(dex);
		buf.writeInt(breath);
		buf.writeInt(maxBreath);
		
		buf.writeInt(atk);
		buf.writeInt(def);
		buf.writeInt(critChance);
		buf.writeInt(critPower);
		
		buf.writeLong(exp);
		buf.writeByte(level);
		
		buf.writeBytes(n);
		n = null;
		return buf;
	}

	@Override
	public Packet fromBuffer(ChannelBuffer buf) {
		int length = buf.readInt()-73;
		byte[] bytes = new byte[length];
		oId = buf.readInt();
		
		x = buf.readFloat();
		y = buf.readFloat();
		w = buf.readInt();
		h = buf.readInt();
		
		hp = buf.readInt();
		maxHp = buf.readInt();
		str = buf.readInt();
		con = buf.readInt();
		dex = buf.readInt();
		breath = buf.readInt();
		maxBreath = buf.readInt();
		
		atk = buf.readInt();
		def = buf.readInt();
		critChance = buf.readInt();
		critPower = buf.readInt();
		
		exp = buf.readLong();
		level = buf.readByte();
		buf.readBytes(bytes);
		name = bytesToString(bytes);
		return this;
	}

	@Override
	public int getLength() {
		return 0;
	}

	@Override
	public boolean isDynamicLength() {
		return true;
	}

	@Override
	public byte getId() {
		return 9;
	}

	public String toString() {
		return "PlayerInfo xy="+x+"_"+y+" ; wh="+w+"_"+h+" ; oId="+oId ;
	}	
	
	@Override
	public byte getPriority() {
		return PRIOR_HIGH;
	}
	
}
